package cn.smartaccount.demo.common.util;

import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;

/**
 * applicationContext 帮助类
 *
 * @author xuluquan
 * @date 2019-12-04 23:57
 */
public class SpringUtil {
    private static ApplicationContext applicationContext = null;

    public static void setApplicationContext(ApplicationContext context) {
        if (SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = context;
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static Environment getEnvironment() {
        if (applicationContext != null) {
            return applicationContext.getEnvironment();
        }
        return null;
    }
}
